package com.example.photography.adapter;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.bean.Product_bean;
import com.example.photography.bean.UrlBean;
import com.example.photography.ui.CircleImageView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HotproductAdapter extends MyBaseAdapter {
    public HotproductAdapter(List list, Context context) {
        super(list, context);
    }

    @Override
    public View myGetView(int position, View convertView) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.hot_product, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //装载数据
        Product_bean productBean = (Product_bean) list.get(position);
        Glide.with(mContext)
                .load(productBean.getHead_url())
                .thumbnail(Glide.with(mContext).load(R.drawable.loading))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))
                .into(viewHolder.ivHead);
        viewHolder.tvName.setText(productBean.getName());
        viewHolder.tvNum.setText("NO." + (position + 1) + "");
        List<UrlBean> urlBeans = JSONObject.parseArray(productBean.getProducts(), UrlBean.class);
        viewHolder.svProduct.setImageURI(urlBeans.get(0).getUrl());
        return convertView;
    }


    static
    class ViewHolder {
        @BindView(R.id.iv_head)
        CircleImageView ivHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.rl_item)
        LinearLayout rlItem;
        @BindView(R.id.sv_product)
        SimpleDraweeView svProduct;
        @BindView(R.id.tv_num)
        TextView tvNum;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
